package org.jeecg.modules.carRecord.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.carRecord.entity.CarRecord;
import org.jeecg.modules.carRecord.service.ICarRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 车辆进出记录
 * @Author: jeecg-boot
 * @Date:   2024-07-24
 * @Version: V1.0
 */
@Api(tags="车辆进出记录")
@RestController
@RequestMapping("/carRecord/carRecord")
@Slf4j
public class CarRecordController extends JeecgController<CarRecord, ICarRecordService> {
	@Autowired
	private ICarRecordService carRecordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param carRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-分页列表查询")
	@ApiOperation(value="车辆进出记录-分页列表查询", notes="车辆进出记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CarRecord carRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CarRecord> queryWrapper = QueryGenerator.initQueryWrapper(carRecord, req.getParameterMap());
		Page<CarRecord> page = new Page<CarRecord>(pageNo, pageSize);
		IPage<CarRecord> pageList = carRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "车辆进出记录-对外-分页列表查询")
	 @ApiOperation(value="车辆进出记录-对外-分页列表查询", notes="车辆进出记录-对外-分页列表查询")
	 @GetMapping(value = "/m/list")
	 public Result<?> mQueryPageList(CarRecord carRecord,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<CarRecord> queryWrapper = QueryGenerator.initQueryWrapper(carRecord, req.getParameterMap());
		 queryWrapper.orderByDesc("create_time");
		 Page<CarRecord> page = new Page<CarRecord>(pageNo, pageSize);
		 IPage<CarRecord> pageList = carRecordService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param carRecord
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-添加")
	@ApiOperation(value="车辆进出记录-添加", notes="车辆进出记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CarRecord carRecord) {
		carRecordService.save(carRecord);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param carRecord
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-编辑")
	@ApiOperation(value="车辆进出记录-编辑", notes="车辆进出记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CarRecord carRecord) {
		carRecordService.updateById(carRecord);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-通过id删除")
	@ApiOperation(value="车辆进出记录-通过id删除", notes="车辆进出记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		carRecordService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-批量删除")
	@ApiOperation(value="车辆进出记录-批量删除", notes="车辆进出记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.carRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "车辆进出记录-通过id查询")
	@ApiOperation(value="车辆进出记录-通过id查询", notes="车辆进出记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CarRecord carRecord = carRecordService.getById(id);
		if(carRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(carRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param carRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CarRecord carRecord) {
        return super.exportXls(request, carRecord, CarRecord.class, "车辆进出记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CarRecord.class);
    }

	@AutoLog(value = "出入场查询-统计")
	@ApiOperation(value = "出入场查询-统计", notes = "出入场查询-统计")
	@GetMapping(value = "/getCount")
	public Result<?> getCount(HttpServletRequest req) {
		List<Map<String, Object>> list = carRecordService.getCount();
		String[] date = new String[list.size()];
		int[] total = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			date[i] = list.get(i).get("time").toString();
			total[i] = Integer.parseInt(list.get(i).get("count").toString());
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("count", total);
		jsonObject.put("date", date);
		return Result.OK(jsonObject);
	}

	@GetMapping(value="/queryCount")
	public Result<?> queryCount(){
		Integer totalCount=carRecordService.count();
		QueryWrapper queryWrapper=new QueryWrapper();
		Integer count=carRecordService.count(queryWrapper);
		BigDecimal bigDecimal=new BigDecimal(totalCount);
		BigDecimal bigDecimal1=new BigDecimal(count);
		BigDecimal rate=bigDecimal1.divide(bigDecimal, 4, RoundingMode.HALF_UP);
		BigDecimal rate1=rate.multiply(new BigDecimal(100));
		String rate2=String.valueOf(rate1).substring(0,String.valueOf(rate1).length()-2)+"%";
		Map<String,Object> map=new HashMap();
		map.put("total1",totalCount);
		map.put("total2",count);
		map.put("total3", rate2);
		return Result.OK(map);
	}

}
